
***************************************************
*Table 1
***************************************************
***************************************************
*Table 1: Baseline sample, Estimate, SA receipt rate and Employment rate
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

g e=1

g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year month b_month b_year) /*!*/

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g age_q0_4= age_q0^4
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3
g dXage_q0_4=d*age_q0_4

*********************************************************************


eststo clear
local model=0
foreach y of varlist sa wage {
preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

preserve
keep if age_q>=21 & age_q<29


sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

forvalues s=1/5 {

*local y="wage"
local spec_num=`s'
if `y'==wage local title="Employment (residuals)"
if `y'==sa   local title="Social Assistance (residuals)"
if `y'==wage local fig_num=6
if `y'==sa local fig_num=2

local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05

if `spec_num'==1 local spec="d age_q0"
if `spec_num'==2 local spec="d age_q0 age_q0_2"
if `spec_num'==3 local spec="d age_q0 age_q0_2 age_q0_3"
if `spec_num'==4 local spec="d age_q0                   dXage_q0"
if `spec_num'==5 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"

if `spec_num'==1 local spec_name="1st order polynomial"
if `spec_num'==2 local spec_name="2nd order polynomial"
if `spec_num'==3 local spec_name="3rd order polynomial"
if `spec_num'==4 local spec_name="1st order polynomial spline"
if `spec_num'==5 local spec_name="2nd order polynomial spline"


local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/table1_1 , addlabel(samplesize, `obs', N_clust,`N_clust',outcome,`y',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

}
restore
}

***************************************************
*Table 1: Cohort-tracking sample, Estimate, SA receipt rate
***************************************************

use "$workdata/cohort", clear /* see $data_do/cohort.do */

g e=1
collapse sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g age_q0_4= age_q0^4
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3
g dXage_q0_4=d*age_q0_4

*********************************************************************


eststo clear
local model=0
foreach y of varlist sa {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

preserve
keep if age_q>=21 & age_q<29
keep if b_year>=1975 & b_year<=1981


sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

forvalues s=1/5 {

local spec_num=`s'
if `y'==sa   local title="Social Assistance (residuals)"
if `y'==sa local fig_num=2

local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05

if `spec_num'==1 local spec="d age_q0"
if `spec_num'==2 local spec="d age_q0 age_q0_2"
if `spec_num'==3 local spec="d age_q0 age_q0_2 age_q0_3"
if `spec_num'==4 local spec="d age_q0                   dXage_q0"
if `spec_num'==5 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"

if `spec_num'==1 local spec_name="1st order polynomial"
if `spec_num'==2 local spec_name="2nd order polynomial"
if `spec_num'==3 local spec_name="3rd order polynomial"
if `spec_num'==4 local spec_name="1st order polynomial spline"
if `spec_num'==5 local spec_name="2nd order polynomial spline"


local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/table1_2 , addlabel(samplesize, `obs', N_clust,`N_clust',outcome,`y',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

}
restore
}

use "$tables/table1_1" , clear
g sample="baseline"
append using "$tables/table1_2"
replace sample="cohort" if sample==""
save "$tables/table1" , replace

***************************************************
*end: Table 1
***************************************************
